
/**
 * 解析身份证号信息
 * @param {string} idCard - 18位身份证号
 * @returns {object} 包含性别、年龄、出生日期的对象
 */
export function parseIdCard(idCard) {
  // 简单验证
  if (!/^\d{17}[\dXx]$/.test(idCard)) {
    throw new Error("请输入有效的18位身份证号");
  }

  // 提取出生日期
  const year = idCard.substring(6, 10);
  const month = idCard.substring(10, 12);
  const day = idCard.substring(12, 14);
  const birthDate = `${year}-${month}-${day}`;

  // 计算年龄
  const today = new Date();
  const birth = new Date(year, month - 1, day);
  let age = today.getFullYear() - birth.getFullYear();
  if (
    today.getMonth() < birth.getMonth() ||
    (today.getMonth() === birth.getMonth() && today.getDate() < birth.getDate())
  ) {
    age--;
  }

  // 提取性别
  const genderCode = idCard.substring(16, 17);
  const gender = genderCode % 2 === 1 ? "男" : "女";

  return {
    gender,
    age,
    birthDate,
  };
}
